import Vue from 'vue'
import Vuex from 'vuex'
import createPersistedState from 'vuex-persistedstate'

Vue.use(Vuex)

const state = {
    checkedCities: [], // 存储选中的城市
    currentOption: { } // 存储当前选中的搜索类型
}

const mutations = {
    SET_CHECKED_CITIES(state, cities) {
        state.checkedCities = cities
    },
    SET_CURRENT_OPTION(state, option) {
        state.currentOption = option
    }
}

const actions = {
    updateCheckedCities({ commit }, cities) {
        commit('SET_CHECKED_CITIES', cities)
    },
    updateCurrentOption({ commit }, option) {
        commit('SET_CURRENT_OPTION', option)
    }
}

const getters = {
    getCheckedCities: state => state.checkedCities,
    getCurrentOption: state => state.currentOption
}

export default new Vuex.Store({
    state,
    mutations,
    actions,
    getters,
    plugins: [
        createPersistedState({
            key: 'search-storage', // 设置存储的key
            paths: ['checkedCities', 'currentOption'] // 持久化checkedCities和currentOption
        })
    ]
})
